// pages/card2/list/list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    api:[
      'park/query',
      'coupon/exp',
      'coupon/byCustomer',
      'act/cusAct',
      'mobileCoupon/getAPPCouponListByType',
    ],
    idx:0,
    type:0,
    name:'卡券',
    list:[{}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.setNavigationBarTitle({title:e.n||'卡券'})
    this.getList(e.idx,e.type);
    this.setData({
      idx:e.idx,
      type:e.type,
      name:e.n
    })
    if(e.idx==1 && e.type ==2) this.unrecive(e.idx,e.type);
  },
  goCode:function(e){
    var t = this;
    if(t.data.idx == 2) return;
    var idx = e.currentTarget.dataset.idx,
      item = t.data.list[idx];
    if(item.status > 1) return;
    wx.navigateTo({
      url:'/pages/card2/code/code?n='+t.data.name+'&item='+JSON.stringify(item)
    })
  },
  unrecive:function(idx,type){
    var t = this,
      appUserInfo = wx.getStorageSync('appUserInfo');
    wx.request({
      url: 'https://live.sgwxmini.com/beauty/coupon/unrecive',
      data: {
        hosId: app.api.siteinfo.hosId,
        customerId: appUserInfo.customerId,
        type: 0
      },
      success: function (e) {
        if(e.data.result == 0){
          var arr = e.data.batches.map(item=>{
            item.expireTime = app.helper.formatData(new Date(item.expireTime))
            item.customerId = appUserInfo.customerId
            return item;
          })
          console.log(arr)
          t.setData({
            list:[...arr,...t.data.list]
          })
        }
      }
    })
  },

  getList:function(idx,type){
    var t = this,
      appUserInfo = wx.getStorageSync('appUserInfo'),
      url = 'https://live.sgwxmini.com/beauty/';
    if(idx==1){
      url += t.data.api[type-0]
    }else {
      url += t.data.api[4]
    }

    wx.request({
      url:url,
      data:{
        hosId:app.api.siteinfo.hosId,
        crmCustomerId:appUserInfo.crmId,
        customerId:appUserInfo.customerId,
        // mobile:appUserInfo.mobile,
        type:type-0+1
      },
      success:function (e) {
        var arr =[];
        if(e.data.result == 0){
          if(idx == 1){
            e.data.coupons && arr.push(...e.data.coupons);
            e.data.cusActs && arr.push(...e.data.cusActs);

            t.setData({
              list:arr.map(item=>{
                item.expireTime = app.helper.formatData(new Date(item.expireTime))
                return item;
              })
            })

          }else if(idx == 2){
            arr.push(...e.data.data.customerCouponList,...e.data.data.customerExpireCouponList);
            t.setData({
              list:arr
            })
          }
        }
      }
    })
  },


  setHidden:function(e){
    var t = this,
      idx = e.currentTarget.dataset.idx,
      list = t.data.list;
    list[idx].show = !list[idx].show
    t.setData({
      list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})